package com.hjm.project.profitDay.detailed.service.impl;

import java.util.List;
import java.util.Map;
import java.util.Vector;

import com.hjm.common.utils.StringUtils;
import com.hjm.project.terminal.detail.domain.TerminalRecordDetailVo;
import org.springframework.stereotype.Service;
import com.hjm.project.profitDay.detailed.mapper.ProfitDayDetailedMapper;
import com.hjm.project.profitDay.detailed.domain.ProfitDayDetailed;
import com.hjm.project.profitDay.detailed.service.IProfitDayDetailedService;
import com.hjm.common.utils.text.Convert;

import javax.annotation.Resource;

/**
 * 分润日结明细Service业务层处理
 *
 * @author maker-platform
 * @date 2021-01-16
 */
@Service
public class ProfitDayDetailedServiceImpl implements IProfitDayDetailedService
{
    @Resource
    private ProfitDayDetailedMapper profitDayDetailedMapper;

    /**
     * 查询分润日结明细
     *
     * @param id 分润日结明细ID
     * @return 分润日结明细
     */
    @Override
    public ProfitDayDetailed selectProfitDayDetailedById(Long id)
    {
        return profitDayDetailedMapper.selectProfitDayDetailedById(id);
    }

    /**
     * 查询分润日结明细列表
     *
     * @param profitDayDetailed 分润日结明细
     * @return 分润日结明细
     */
    @Override
    public List<ProfitDayDetailed> selectProfitDayDetailedList(ProfitDayDetailed profitDayDetailed)
    {
        return profitDayDetailedMapper.selectProfitDayDetailedList(profitDayDetailed);
    }

    /**
     * 查询卡费返现列表
     *
     * @param profitDayDetailed 分润日结明细
     * @return 分润日结明细
     */
    @Override
    public List<ProfitDayDetailed> selectCardFeeList(ProfitDayDetailed profitDayDetailed)
    {
        return profitDayDetailedMapper.selectCardFeeList(profitDayDetailed);
    }
    @Override
    public Map selectCardFeeSum(ProfitDayDetailed profitDayDetailed)
    {
        return profitDayDetailedMapper.selectCardFeeSum(profitDayDetailed);
    }

    /**
     * 查询卡费返现列表 导出
     *
     * @param profitDayDetailed 分润日结明细
     * @return 分润日结明细
     */
    @Override
    public List<TerminalRecordDetailVo> selectCardFeeListVo(ProfitDayDetailed profitDayDetailed)
    {
        return profitDayDetailedMapper.selectCardFeeListVo(profitDayDetailed);
    }

    @Override
    public  List<Map<String,Object>> selectProfitDayDetailedListSum(ProfitDayDetailed profitDayDetailed)
    {
/*
        List<Map<String,Object>> list =new ArrayList<>();
*/

        profitDayDetailed.setPayType("2");
        Map<String, Object> params = profitDayDetailed.getParams();

        //根据添加日期
        if (StringUtils.isNotEmpty(profitDayDetailed.getStartDate())) {
            params.put("beginTradeTime",profitDayDetailed.getStartDate());
        }
        if (StringUtils.isNotEmpty(profitDayDetailed.getEndDate())) {
            params.put("endTradeTime",profitDayDetailed.getEndDate());
        }

        Vector<Map<String,Object>> list = new Vector<>();
       /* CompletableFuture<Void> ptype = CompletableFuture.runAsync(() -> {
            //交易查询(本级)
            Map<String, Object> trade = profitDayDetailedMapper.selectSumTrade(profitDayDetailed);
            trade.put("PTYPE", "2");
            list.add(trade);
        });

        CompletableFuture<Void> ptype1 = CompletableFuture.runAsync(() -> {
            profitDayDetailed.setPayType("0");
            profitDayDetailed.getParams().put("must_ptype",0);
            Map<String,Object> one=profitDayDetailedMapper.selectProfitDayDetailedListSum(profitDayDetailed);
            one.put("PTYPE","0");
            list.add(one);
        });


        CompletableFuture<Void> ptype2 = CompletableFuture.runAsync(() -> {
            profitDayDetailed.setPayType("1");
            profitDayDetailed.getParams().put("must_ptype",1);
            Map<String,Object> group=profitDayDetailedMapper.selectProfitDayDetailedListSum(profitDayDetailed);
            group.put("PTYPE","1");
            list.add(group);
        });

        CompletableFuture<Void> voidCompletableFuture = CompletableFuture.allOf(ptype,ptype1, ptype2);
        try {
            voidCompletableFuture.get();
        } catch (Exception e) {
            e.printStackTrace();
        }

        */

        //交易查询(本级)
        Map<String, Object> trade = profitDayDetailedMapper.selectSumTrade(profitDayDetailed);
        trade.put("PTYPE", "2");
        list.add(trade);

        profitDayDetailed.setPayType("0");
        profitDayDetailed.getParams().put("must_ptype",0);
        Map<String,Object> one=profitDayDetailedMapper.selectProfitDayDetailedListSum(profitDayDetailed);
        one.put("PTYPE","0");
        list.add(one);

        profitDayDetailed.setPayType("1");
        profitDayDetailed.getParams().put("must_ptype",1);
        Map<String,Object> group=profitDayDetailedMapper.selectProfitDayDetailedListSum(profitDayDetailed);
        group.put("PTYPE","1");
        list.add(group);


        return list;
    }

    /**
     * 新增分润日结明细
     *
     * @param profitDayDetailed 分润日结明细
     * @return 结果
     */
    @Override
    public int insertProfitDayDetailed(ProfitDayDetailed profitDayDetailed)
    {
        return profitDayDetailedMapper.insertProfitDayDetailed(profitDayDetailed);
    }

    /**
     * 修改分润日结明细
     *
     * @param profitDayDetailed 分润日结明细
     * @return 结果
     */
    @Override
    public int updateProfitDayDetailed(ProfitDayDetailed profitDayDetailed)
    {
        return profitDayDetailedMapper.updateProfitDayDetailed(profitDayDetailed);
    }

    /**
     * 删除分润日结明细对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteProfitDayDetailedByIds(String ids)
    {
        return profitDayDetailedMapper.deleteProfitDayDetailedByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除分润日结明细信息
     *
     * @param id 分润日结明细ID
     * @return 结果
     */
    @Override
    public int deleteProfitDayDetailedById(Long id)
    {
        return profitDayDetailedMapper.deleteProfitDayDetailedById(id);
    }
}
